package com.woniu.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.user.domain.CustomerCoupon;
import com.woniu.user.service.ICustomerCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户优惠券Controller
 *
 * @author yy
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/user/tbcustomercoupon")
@Api("用户优惠券管理")
public class CustomerCouponController extends BaseController {
    @Autowired
    private ICustomerCouponService customerCouponService;

    /**
     * 查询用户优惠券列表
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:list')")
    @GetMapping("/list")
    @ApiOperation("用户优惠券分页查询")
    public TableDataInfo list(CustomerCoupon customerCoupon) {
        startPage();
        List<CustomerCoupon> list = customerCouponService.selectCustomerCouponList(customerCoupon);
        return getDataTable(list);
    }

    /**
     * 导出用户优惠券列表
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:export')")
    @Log(title = "用户优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("用户优惠券导出")
    public void export(HttpServletResponse response, CustomerCoupon customerCoupon) {
        List<CustomerCoupon> list = customerCouponService.selectCustomerCouponList(customerCoupon);
        ExcelUtil<CustomerCoupon> util = new ExcelUtil<CustomerCoupon>(CustomerCoupon.class);
        util.exportExcel(response, list, "用户优惠券数据");
    }

    /**
     * 获取用户优惠券详细信息
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("根据主键查询用户优惠券")
    @ApiImplicitParam(name = "ids", value = "用户ID", required = true, dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerCouponService.selectCustomerCouponById(id));
    }

    /**
     * 新增用户优惠券
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:add')")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加用户优惠券")
    public AjaxResult add(@RequestBody CustomerCoupon customerCoupon) {
        return toAjax(customerCouponService.insertCustomerCoupon(customerCoupon));
    }

    /**
     * 修改用户优惠券
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:edit')")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("更新用户优惠券")
    public AjaxResult edit(@RequestBody CustomerCoupon customerCoupon) {
        return toAjax(customerCouponService.updateCustomerCoupon(customerCoupon));
    }

    /**
     * 删除用户优惠券
     */
    //@PreAuthorize("@ss.hasPermi('apartment:tbcustomercoupon:remove')")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除用户优惠券")
    @ApiImplicitParam(name = "ids", value = "主键数组", required = true, dataType = "Long")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(customerCouponService.deleteCustomerCouponByIds(ids));
    }

    @RequestMapping("/feign/getCustomerCoupon")
    public List<Integer> getCustomerCoupon(@RequestParam("customerId") Integer customerId) {
       return customerCouponService.queryCustomerCoupon(customerId);

    }

    @RequestMapping("/feign/editCustomerCouponStatus")
    public Integer editCustomerCouponStatus(@RequestParam("customerId") Integer customerId,@RequestParam("couponId") Integer couponId){
       return customerCouponService.modifyCustomerCouponStatus(customerId,couponId);
    }
    @RequestMapping("/feign/getByCustomerIdAndCouponId")
    public CustomerCoupon getByCustomerIdAndCouponId(@RequestParam("customerId") Integer customerId,@RequestParam("couponId") Integer couponId){
        return customerCouponService.queryByCustomerIdAndCouponId(customerId,couponId);
    }
}
